//
//  YMLoginViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMLoginViewController.h"
#import "YMLoginSegementView.h"
#import "IQKeyboardManager.h"
#import "YMMobileLoginView.h"
#import "YMPasswordLoginView.h"
#import "YMRegisterViewController.h"
#import "YMMainPrivacyAlert.h"


@interface YMLoginViewController ()

@property (nonatomic, strong) UIImageView *imgvBg;
@property (nonatomic, strong) UIButton *btnSign;
@property (nonatomic, strong) UIImageView *imgvLogo;
@property (nonatomic, strong) UILabel *lblTitle;

@property (nonatomic, strong) YMLoginSegementView *vSegement;
@property (nonatomic, strong) UIScrollView *svMain;
@property (nonatomic, strong) UIView *vContent;
/** 验证码登陆 */
@property (nonatomic, strong) YMMobileLoginView *vMobile;
/** 密码登陆 */
@property (nonatomic, strong) YMPasswordLoginView *vPwd;

@property (nonatomic, strong) UIButton *btnAgree;
@property (nonatomic, strong) YYLabel *lblAgree;
@property (nonatomic, strong) UIButton *btnLogin;

@property (nonatomic, strong) UIButton *btnForget;
@property (nonatomic, strong) UILabel *lblVersion;

@property (nonatomic, assign) BOOL isMobileCan;
@property (nonatomic, assign) BOOL isAccountCan;

@end

@implementation YMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    [self loadNoLoginTuotuToken];
    [YMMainPrivacyAlert checkShowPrivacy];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

#pragma mark -- UI
- (void)setUI{
    self.navBarHidden = YES;
    [self.view addSubview:self.imgvBg];
    [self.view addSubview:self.btnSign];
    [self.view addSubview:self.imgvLogo];
    [self.view addSubview:self.lblTitle];
    [self.view addSubview:self.vSegement];
    [self.view addSubview:self.svMain];
    [self.svMain addSubview:self.vContent];
    [self.vContent addSubview:self.vMobile];
    [self.vContent addSubview:self.vPwd];
    [self.view addSubview:self.btnAgree];
    [self.view addSubview:self.lblAgree];
    [self.view addSubview:self.btnLogin];
    [self.view addSubview:self.btnForget];
    [self.view addSubview:self.lblVersion];
    
    [self.imgvBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.btnSign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo(STATUS_BAR_HEIGHT);
        make.height.mas_equalTo(NAV_BAR_HEIGHT);
    }];
    [self.imgvLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:30]+TOP_BAR_HEIGHT);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:90 height:90]);
    }];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.imgvLogo.mas_bottom).offset([SFitUtil fit:12]);
    }];
    [self.vSegement mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
        make.top.equalTo(self.imgvLogo.mas_bottom).offset([SFitUtil fit:62]);
    }];
    [self.svMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.vSegement.mas_bottom).offset([SFitUtil fit:20]);
        make.height.mas_equalTo([YMMobileLoginView getViewHeight]);
    }];
    [self.vContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.height.equalTo(self.svMain);
        make.right.mas_equalTo(self.vPwd);
    }];
    [self.vMobile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    [self.vPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(self.vMobile.mas_right);
        make.width.equalTo(self.vMobile);
    }];
    [self.btnAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.svMain.mas_bottom).offset([SFitUtil fit:10]);
        make.height.mas_equalTo([SFitUtil fit:34]);
        make.width.mas_equalTo([SFitUtil fit:12+14]);
        make.left.mas_equalTo([SFitUtil fit:9]);
    }];
    [self.lblAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btnAgree.mas_right);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.centerY.equalTo(self.btnAgree);
    }];
    [self.btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btnAgree.mas_bottom).offset([SFitUtil fit:10]);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
        make.centerX.mas_equalTo(0);
    }];
    [self.btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT+[SFitUtil fit:-38]);
        make.size.mas_equalTo([SFitUtil fitSize:120 height:50]);
        make.centerX.mas_equalTo(0);
    }];
    [self.lblVersion mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.btnForget.mas_bottom).offset([SFitUtil fit:12]);
    }];
    
}

//脱兔未登录 token
- (void)loadNoLoginTuotuToken{
    [YMLoginService requestNoLoginGetTuoTuTokenSuccess:^{} failure:^(NSString * _Nonnull message) {
        
    }];
}

#pragma mark -- event action
- (void)registerClickAction:(id)sender{
   //注册
    YMRegisterViewController *vc = [[YMRegisterViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
- (void)clickAgreeUrl:(NSInteger)type{
    //跳转协议
    [YMBridging goToProtocolVC:type];
    
}
- (void)agreeAction:(id)sender{
    self.btnAgree.selected = !self.btnAgree.isSelected;
    self.vMobile.isAgree = self.btnAgree.isSelected;
    self.vPwd.isAgree = self.btnAgree.isSelected;
}

- (void)loginClickAction:(id)sender{
    //登录
    if (_vSegement.indexNow == 0){
        [self.vMobile loginClickAction];
    }else{
        [self.vPwd loginClickAction];
    }
}

- (void)forgetClickAction:(id)sender{
    //忘记密码
    [YMBridging goToForgetPwdVC];
}

#pragma mark -- setter and getter
- (UIImageView *)imgvBg{
    if (_imgvBg == nil){
        _imgvBg = [[UIImageView alloc] init];
        _imgvBg.image = [UIImage imageNamed:@"login_background_image"];
    }
    return _imgvBg;
}
- (UIButton *)btnSign{
    if (_btnSign == nil) {
        _btnSign = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSign.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnSign setTitleColor:[SUIUtil colorOf:0x000000] forState:UIControlStateNormal];
        [_btnSign setTitle:KLanguage(@"im_register_txt") forState:UIControlStateNormal];
        [_btnSign addTarget:self action:@selector(registerClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnSign;
}
- (UIImageView *)imgvLogo{
    if (_imgvLogo == nil) {
        _imgvLogo = [[UIImageView alloc] init];
        _imgvLogo.image = [UIImage imageNamed:@"tools_app_logo_icon"];
    }
    return _imgvLogo;
}
- (UILabel *)lblTitle{
    if (_lblTitle == nil) {
        _lblTitle = [SThemeUtil boldLabel:KLanguage(@"login_txt") color:0x333333 fontSize:18];
    }
    return _lblTitle;
}
- (YMLoginSegementView *)vSegement{
    if (_vSegement == nil){
        _vSegement = [[YMLoginSegementView alloc] init];
        WEAK_SELF;
        _vSegement.segementClickBlock = ^(NSInteger index) {
            [weakSelf.svMain setContentOffset:CGPointMake(SCREEN_WIDTH*index, 0) animated:YES];
            weakSelf.btnForget.hidden = index==0 ? YES:NO;
        };
    }
    return _vSegement;
}
- (UIScrollView *)svMain{
    if (_svMain == nil) {
        _svMain = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _svMain.scrollEnabled = NO;
        _svMain.showsVerticalScrollIndicator = NO;
        _svMain.showsHorizontalScrollIndicator = NO;
        _svMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _svMain;
}
- (UIView *)vContent{
    if (_vContent == nil) {
        _vContent = [[UIView alloc] init];
    }
    return _vContent;
}
- (YMMobileLoginView *)vMobile{
    if (_vMobile == nil) {
        _vMobile = [[YMMobileLoginView alloc] init];
        WEAK_SELF;
        _vMobile.changeButtonStautsBlock = ^(BOOL isEnable) {
            weakSelf.isMobileCan = isEnable;
        };
    }
    return _vMobile;
}
- (YMPasswordLoginView *)vPwd{
    if (_vPwd == nil) {
        _vPwd = [[YMPasswordLoginView alloc] init];
        WEAK_SELF;
        _vPwd.changeButtonStautsBlock = ^(BOOL isEnable) {
            weakSelf.isAccountCan = isEnable;
        };
    }
    return _vPwd;
}
- (YYLabel *)lblAgree{
    if (!_lblAgree) {
        _lblAgree = [[YYLabel alloc] init];
        _lblAgree.textColor = STheme.D4;
        _lblAgree.font = [SFitUtil fitFont:14];
        _lblAgree.textAlignment = NSTextAlignmentLeft;
        _lblAgree.numberOfLines = 0;
        _lblAgree.preferredMaxLayoutWidth = SCREEN_WIDTH-[SFitUtil fit:35+15];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:[SFitUtil fit:4]];
        NSDictionary *attDict = @{NSParagraphStyleAttributeName:paragraphStyle};
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:KLanguage(@"readed_agreed_txt") attributes:attDict];
        NSString *readText1 = [NSString stringWithFormat:@"%@%@",KLanguage(@"ag_hint_2_txt"),KLanguage(@"and_txt")];
        WEAK_SELF;
        NSMutableAttributedString *read1 = [[NSMutableAttributedString alloc] initWithString:readText1];
        [read1 setTextHighlightRange:NSMakeRange(0, readText1.length) color:STheme.D12 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:1];
        }];
        [att appendAttributedString:read1];
        
        NSString *readText2 = KLanguage(@"ag_hint_3_txt");
        NSMutableAttributedString *read2 = [[NSMutableAttributedString alloc] initWithString:readText2];
        [read2 setTextHighlightRange:NSMakeRange(0, readText2.length) color:STheme.D12 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:2];
        }];
        [att appendAttributedString:read2];
        _lblAgree.attributedText = att;
    }
    return _lblAgree;
}
- (UIButton *)btnAgree {
    if (_btnAgree == nil) {
        _btnAgree = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnAgree setImage:[UIImage imageNamed:@"login_agreement_normal"] forState:UIControlStateNormal];
        [_btnAgree setImage:[UIImage imageNamed:@"login_agreement_selected"] forState:UIControlStateSelected];
        [_btnAgree addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btnAgree g_clickEdgeWithTop:10 bottom:10 left:10 right:10];
    }
    return _btnAgree;
}
- (UIButton *)btnLogin{
    if (_btnLogin == nil) {
        _btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLogin.titleLabel.font = [SFitUtil fitBoldFont:18];
        [_btnLogin setTitleColor:STheme.D9 forState:UIControlStateNormal];
        [_btnLogin setTitle:KLanguage(@"login_txt") forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(loginClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnLogin.clipsToBounds = YES;
        _btnLogin.layer.cornerRadius = [SFitUtil fit:8];
        [_btnLogin setBackgroundImage:[UIImage imageWithColor:STheme.D12] forState:UIControlStateSelected];
        [_btnLogin setBackgroundImage:[UIImage imageWithColor:[SUIUtil colorOf:STheme.H_D12 alpha:0.5]] forState:UIControlStateNormal];
        _btnLogin.selected = YES;
    }
    return _btnLogin;
}
- (UIButton *)btnForget{
    if (_btnForget == nil) {
        _btnForget = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnForget.titleLabel.font = [SFitUtil fitFont:16];
        [_btnForget setTitleColor:STheme.D12 forState:UIControlStateNormal];
        [_btnForget setTitle:KLanguage(@"forget_pwd_txt") forState:UIControlStateNormal];
        [_btnForget addTarget:self action:@selector(forgetClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnForget.clipsToBounds = YES;
        _btnForget.layer.cornerRadius = [SFitUtil fit:25];
        _btnForget.backgroundColor = STheme.D9;
        _btnForget.hidden = YES;
    }
    return _btnForget;
}
- (UILabel *)lblVersion{
    if (_lblVersion == nil){
        _lblVersion = [SThemeUtil label:@"" color:STheme.H_D12 fontSize:14];
        _lblVersion.text = @"Vi 1";
    }
    return _lblVersion;
}


@end
